﻿Public Class cFakeServer
    Inherits cConnectionBase

    Class cpacket
        Public ticks As UInt64
        Public packet() As Byte
    End Class

    Dim GServer As Boolean = True
    Dim pcol As New Collections.Generic.Queue(Of cpacket)
    Dim autosend As Boolean = False
    Sub New()
        autosend = False
    End Sub

    Sub New(ByVal logpath As String)
        autosend = True
        Using r As New IO.StreamReader(New IO.FileStream(logpath, IO.FileMode.Open))
            Dim line As String
            Dim startfound As Boolean = False
            Do

                line = r.ReadLine
                If line = "" Then Exit Do

                Dim lines() As String = line.Split("|")

                Dim ticks As UInt64 = lines(0)
                Dim _from As String = lines(1)
                Dim data() As String = lines(2).Split("-")
                Dim packet(data.GetUpperBound(0)) As Byte
                For i As Int32 = 0 To data.GetUpperBound(0)
                    packet(i) = "&H" & data(i)
                Next
                If startfound = False Then
                    Dim opc As UInt16 = BitConverter.ToUInt16(packet, 2)
                    If opc = &H34A5 Then
                        startfound = True
                    End If
                End If
                If startfound = True Then
                    If _from = "ToClientLogger" Then
                        Dim p As New cpacket
                        p.packet = packet
                        p.ticks = ticks
                        pcol.Enqueue(p)
                    End If
                End If
            Loop
            If startfound = False Then
                Throw New Exception("start not found ")
            End If
        End Using


    End Sub

    Public Overrides Sub Disconnect()
        Set_Status(eSocketStatus.Offline)
    End Sub

    Public Overrides Sub Send(ByRef packet() As Byte)
        Dim opc As UInt16 = BitConverter.ToUInt16(packet, 2)

        Select Case opc
            Case &H2001
                If GServer = False Then
                    Add_Packet(New Byte() {&H10, &H0, &H1, &H20, &H1, &H0, &HD, &H0, &H47, &H61, &H74, &H65, &H77, &H61, &H79, &H53, &H65, &H72, &H76, &H65, &H72, &H0})
                Else
                    Add_Packet(New Byte() {&HE, &H0, &H1, &H20, &H1, &H0, &HB, &H0, &H41, &H67, &H65, &H6E, &H74, &H53, &H65, &H72, &H76, &H65, &H72, &H0})
                End If
            Case &H6100
                Add_Packet(New Byte() {&H5, &H0, &HD, &H60, &H1, &H0, &H1, &H1, &H0, &H5, &H20})
                Add_Packet(New Byte() {&HB, &H0, &HD, &H60, &H1, &H0, &H0, &H1, &H0, &H1, &H8, &HA, &H5, &H0, &H0, &H0, &H2})
                Add_Packet(New Byte() {&H5, &H0, &HD, &H60, &H1, &H0, &H1, &H1, &H0, &H5, &H60})
                Add_Packet(New Byte() {&H6, &H0, &HD, &H60, &H1, &H0, &H0, &H3, &H0, &H2, &H0, &H2})
                Add_Packet(New Byte() {&H5, &H0, &HD, &H60, &H1, &H0, &H1, &H1, &H0, &H0, &HA1})
                Add_Packet(New Byte() {&H2, &H0, &HD, &H60, &H1, &H0, &H0, &H1})
            Case &H6101
                Add_Packet(New Byte() {&H4C, &H0, &H1, &HA1, &H1, &H0, &H1, &H39, &H13, &H0, &H53, &H52, &H4F, &H5F, &H52, &H75, &H73, &H73, &H69, &H61, &H5F, &H4F, &H66, &H66, &H69, &H63, &H69, &H61, &H6C, &H0, &H1, &HDC, &H0, &H4, &H0, &HC7, &HE5, &HE2, &HF1, &H80, &H8, &HC4, &H9, &H1, &H1, &HDD, &H0, &H6, &H0, &HC2, &HE5, &HED, &HE5, &HF0, &HE0, &HDF, &H8, &HC4, &H9, &H1, &H1, &HE0, &H0, &HB, &H0, &H49, &H73, &H5F, &H63, &H6F, &H6D, &H69, &H6E, &H67, &H2E, &H2E, &H0, &H0, &HC4, &H9, &H0, &H0})
                Add_Packet(New Byte() {&H12, &H0, &H2, &HA1, &H1, &H1, &H1, &HB7, &H1, &H0, &H0, &H9, &H0, &H31, &H32, &H37, &H2E, &H30, &H2E, &H30, &H2E, &H31, &HA2, &H3D})
            Case &H6103
                Add_Packet(New Byte() {5, &H0, &HD, &H60, &H1, &H0, &H1, &H1, &H0, &H5, &H20})
                Add_Packet(New Byte() {&HB, &H0, &HD, &H60, &H1, &H0, &H0, &H1, &H0, &H1, &H12, &HA, &H5, &H0, &H0, &H0, &H2})
                Add_Packet(New Byte() {5, &H0, &HD, &H60, &H1, &H0, &H1, &H1, &H0, &H5, &H60})
                Add_Packet(New Byte() {6, &H0, &HD, &H60, &H1, &H0, &H0, &H3, &H0, &H2, &H0, &H2})
                Add_Packet(New Byte() {1, &H0, &H3, &HA1, &H1, &H1, &H1})
            Case &H7007 'char info 
                ' Add_Packet(New Byte() {&H53, &H0, &H7, &HB0, &H1, &H0, &H2, &H1, &H1, &H84, &H7, &H0, &H0, &H4, &H0, &HF1, &HE0, &HF0, &HED, &H0, &HC, &H64, &H25, &H0, &H0, &H0, &H0, &H0, &H0, &H40, &H0, &H1F, &H0, &H0, &H0, &H45, &H2, &H0, &H0, &H24, &H1, &H0, &H0, &H0, &H0, &H0, &H0, &H8, &H14, &H4, &H0, &H0, &H0, &H7B, &H5, &H0, &H0, &H0, &H57, &H5, &H0, &H0, &H2, &HC3, &H5, &H0, &H0, &H2, &HA3, &H4, &H0, &H0, &H0, &HED, &H4, &H0, &H0, &H0, &HDA, &H0, &H0, &H0, &H0, &H3E, &H0, &H0, &H0, &H0, &H0})
                'Case &H7001 'charselct ?
                Add_Packet(New Byte() {1, &H0, &H1, &HB0, &H1, &H0, &H1})
                'start send form log
                'log entry point --> 00-00-A5-34-01-00
                If autosend = True Then
                    Dim thph As New Threading.Thread(AddressOf psender)
                    thph.Start()
                End If

        End Select


    End Sub

    Sub psender()
        Dim firstticks As UInt64
        Dim firstnowticks As UInt64

        Dim p As cpacket = pcol.Dequeue
        firstticks = p.ticks
        firstnowticks = Now.Ticks

        Add_Packet(p.packet)
        Do Until pcol.Count = 0
            Threading.Thread.Sleep(2)
            Dim tickspassde As UInt64 = Now.Ticks - firstnowticks
            Dim ptickspassed As UInt64 = pcol.Peek.ticks - firstticks

            If tickspassde >= ptickspassed Then
                p = pcol.Dequeue
                Add_Packet(p.packet)
            End If

        Loop





    End Sub

    Public Overrides Sub Start(ByVal ip As String, ByVal port As UInteger)
        Set_Status(eSocketStatus.Connected)

        If GServer = True Then
            GServer = False
        Else
            GServer = True
        End If
    End Sub
End Class
